package com.segi.view.refresh;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.segi.view.R;
import com.segi.view.xlistview.GradientView;

/**
 * 消息中心自定义下拉刷新的圆圈和箭头
 *
 * @author tangjl
 * @date 2018/6/22.
 */

public class HeaderLayoutThreeType extends LoadingLayout {

    private RotateAnimation mRotateAnimation;
    private TextView ringRefreshStatus,mGradientTxt;
    private GradientView mGradientView;
    private RelativeLayout mHeaderContainer;

    public HeaderLayoutThreeType(Context context) {
        super(context);
        init();
    }

    public HeaderLayoutThreeType(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mHeaderContainer = (RelativeLayout) findViewById(R.id.header_content);
        mGradientView = (GradientView) findViewById(R.id.gradient_header_view);
        ringRefreshStatus = (TextView) findViewById(R.id.gradient_header_hint_text);
        mGradientTxt = (TextView) findViewById(R.id.gradient_txt);
        mRotateAnimation = (RotateAnimation) AnimationUtils.loadAnimation(mContext, R.anim.rotating);
        LinearInterpolator lir = new LinearInterpolator();
        mRotateAnimation.setInterpolator(lir);
        mGradientView.setProgress(90);
    }

    @Override
    public int getContentSize() {
        if (null != mHeaderContainer) {
            return mHeaderContainer.getHeight();
        }
        return (int) (getResources().getDisplayMetrics().density * 60);
    }

    @Override
    protected View createLoadingView(Context context, AttributeSet attrs) {
        View container = LayoutInflater.from(context).inflate(R.layout.vw_header_v2_gradient, null);
        return container;
    }

    /**
     * 下拉可以刷新
     */
    @Override
    public void onPullToRefresh() {
        ringRefreshStatus.setText(R.string.header_hint_refresh_normal);
        mGradientView.setIsShowIcon(true);
    }

    /**
     * 松开后刷新
     */
    @Override
    public void onReleaseToRefresh() {
        ringRefreshStatus.setText(R.string.header_hint_refresh_ready);
        mGradientView.setIsShowIcon(false);
    }

    /**
     * 下拉中
     *
     * @param scaleOfLayout
     */
    @Override
    public void onPull(float scaleOfLayout) {
        setViewVisibility();
        scaleOfLayout = scaleOfLayout > 1.0f ? 1.0f : scaleOfLayout;
        int progress = (int) ((scaleOfLayout) * 100);
        mGradientView.setProgress(progress > 90 ? 90 : progress);
        mGradientView.setArrowAlpha((progress * 2) + 55);
    }

    /**
     * 正在刷新
     */
    @Override
    public void onRefreshing() {
        setViewVisibility();
        mGradientView.setIsShowIcon(false);
        ringRefreshStatus.setText(R.string.header_hint_refresh_loading);
        mGradientView.startAnimation(mRotateAnimation);
    }

    @Override
    protected void onRefreshed(String txtSize) {
        //刷新完成
        ringRefreshStatus.setVisibility(GONE);
        mGradientView.clearAnimation();
        mGradientView.setVisibility(GONE);
        mGradientTxt.setText(txtSize);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(mGradientTxt.getWidth(), ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);//addRule参数对应RelativeLayout XML布局的属性
        mGradientTxt.setLayoutParams(layoutParams);
        mGradientTxt.setBackgroundColor(ContextCompat.getColor(mContext,R.color.refresh_top));
        Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.refresh_show);
        mGradientTxt.startAnimation(animation);
    }

    @Override
    public void onReset() {
        mGradientView.clearAnimation();
    }

    @Override
    public void setPullLabel(CharSequence pullLabel) {

    }

    @Override
    public void setRefreshingLabel(CharSequence refreshingLabel) {

    }

    @Override
    public void setReleaseLabel(CharSequence releaseLabel) {

    }

    /**
     * 刷新的时候初始化数据
     */
    private void setViewVisibility(){
        mGradientView.setVisibility(VISIBLE);
        ringRefreshStatus.setVisibility(VISIBLE);
        mGradientTxt.setText("");
        mGradientTxt.setBackgroundColor(ContextCompat.getColor(mContext,R.color.refresh_transparent));
    }
}
